#include "SudokuHiddenSingles.h"

SudokuHiddenSingles::SudokuHiddenSingles(QObject* parent) :
        SudokuAbstractSolvingTechnique("Hidden Singles", HiddenSingles, FindValue, parent)
{
}

int SudokuHiddenSingles::solve()
{
    //TODO house iterator
    for(int i=0; i<mSudokuData->getSize(); ++i)//cells in row/col/box from 1 to size
    {
        findHiddenSinglesInHouse(mSudokuData->houseAtRow(i));
        findHiddenSinglesInHouse(mSudokuData->houseAtCol(i));
        findHiddenSinglesInHouse(mSudokuData->houseAtBox(i));
    }

    qDebug() << "SudokuHiddenSingles::solve mCount: " << mCount;
    return mCount;
}

void SudokuHiddenSingles::findHiddenSinglesInHouse(const SudokuHouse* house)
{
    //qDebug() << "SudokuHiddenSingles::findHiddenSinglesInHouse>";

    for(int number = 0; number<mSudokuData->getSize(); ++number)
    {
        int possibleCells = 0;
        int cellIndex = 0;
        int i=0;

        while (i<house->size() && possibleCells < 2)
        {
            if(house->at(i)->isEmpty() && house->at(i)->getPossibl(number))
            {
                ++possibleCells;
                cellIndex = i;
            }
            else if(house->at(i)->getValue() == number+1)//if number already set
            {
                possibleCells = 2;//just to break loop
            }
            ++i;
        }
        if(possibleCells == 1)
        {
            mSudokuData->onFoundValue(house->at(cellIndex)->getRow(), house->at(cellIndex)->getCol(), number+1, HiddenSingles);
            ++mCount;
        }
    }
    //qDebug() << "SudokuHiddenSingles::findHiddenSinglesInHouse<";
}

